package com.ishansong.ticket.service.impl;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

import com.ishansong.ticket.utils.DateUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ishansong.ticket.exception.ServiceDaoException;
import com.ishansong.ticket.exception.ServiceException;
import com.ishansong.ticket.model.TicketRecord;
import com.ishansong.ticket.service.TicketRecordService;
import com.matrixjoy.dal.dao.Dao;
import com.matrixjoy.dal.dao.exception.DaoException;



@Component( value= "ticketRecordService" ) 
public class TicketRecordServiceImpl implements TicketRecordService {

	
	@Autowired
	private Dao dao;

	@Override
	public Boolean deleteTicketRecord(Long id) throws ServiceDaoException, ServiceException {
		try {
			return (Boolean) dao.delete(TicketRecord.class, id);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}



	@Override
	public TicketRecord getTicketRecord(Long id) throws ServiceDaoException, ServiceException {
		try {
			if (null == id) {
				return null;
			}
			return (TicketRecord) dao.get(TicketRecord.class, id);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public Long saveTicketRecord(TicketRecord ticketRecord) throws ServiceDaoException, ServiceException {
		try {
			return (Long) dao.save(ticketRecord);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public void updateTicketRecord(TicketRecord ticketRecord) throws ServiceDaoException, ServiceException {
		try {
			dao.update(ticketRecord);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}

	}

	@Override
	public List<TicketRecord> queryTicketRecordsByTicketId(long ticketId) throws ServiceDaoException, ServiceException {
		try {
			@SuppressWarnings("unchecked")
			List<Serializable> ids = dao.getIdList("queryTicketRecordsByTicketId", new Object[] { ticketId });
			if (!CollectionUtils.isEmpty(ids)) {
				List<TicketRecord> ticketRecords = dao.getList(TicketRecord.class, ids);
				return ticketRecords;
			} else {
				return Collections.emptyList();
			}
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public TicketRecord queryTicketRecordsByTicketIdAndOperatorId(long ticketId, String operatorId, int status) throws ServiceDaoException, ServiceException {
		try {
			@SuppressWarnings("unchecked")
			List<Serializable> ids = dao.getIdList("queryTicketRecordsByTicketIdAndOperatorId", new Object[] {ticketId,status,operatorId});
			if (!CollectionUtils.isEmpty(ids)) {
				List<TicketRecord> ticketRecords = dao.getList(TicketRecord.class, ids);
				return ticketRecords.get(0);
			} else {
				return null;
			}
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public List<TicketRecord> queryTicketRecordsByOperatorIdAndStatus(String operatorId, int status) throws ServiceDaoException, ServiceException {
		try {
			@SuppressWarnings("unchecked")
			List<Serializable> ids = dao.getIdList("queryTicketRecordsByOperatorIdAndStatus", new Object[] { operatorId, status, DateUtil.getTime()});
			if (!CollectionUtils.isEmpty(ids)) {
				List<TicketRecord> ticketRecords = dao.getList(TicketRecord.class, ids);
				return ticketRecords;
			} else {
				return Collections.emptyList();
			}
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public int queryTicketMyCountByStatus(Integer status, String operatorId, String time) throws ServiceDaoException, ServiceException {
		try {
			return dao.count("queryTicketRecordsByOperatorIdAndStatus", new Object[]{operatorId,status,time});
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}
	public Dao getDao() {
		return dao;
	}

	public void setDao(Dao dao) {
		this.dao = dao;
	}

}
